package com.talking.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.talking.App;
import com.talking.adapter.MomentAdapter;
import com.talking.bean.MomentBean;
import com.talking.databinding.ActivityMomentBinding;
import com.talking.presenter.MomentPresenter;
import com.talking.presenter.UserPresenter;
import com.talking.view.main.MainActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MomentActivity extends AppCompatActivity {
    private ActivityMomentBinding binding;
    private MomentAdapter adp;
    private final List<MomentBean> data=new ArrayList<>();
    private ActivityResultLauncher<Intent> launcher;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivityMomentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        binding.momentFab.setOnClickListener(v -> launcher.launch(new Intent(MomentActivity.this,WriteMomentActivity.class)));
        adp=new MomentAdapter(this, data, this::refresh);
        binding.momentList.setAdapter(adp);
        if (!App.momentAccount.equals(App.sp.getString("account",""))){
            binding.momentFab.setVisibility(View.GONE);
        }
        refresh();
        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> refresh());
    }
    private void refresh() {
        data.clear();
        String url;
        if (App.momentAccount.equals("")){
            url="moment/query/";
        }else {
            url="moment/queryPerson/?account="+App.momentAccount;
        }
        MomentPresenter.doget(url, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                runOnUiThread(() -> {
                    try {
                        JSONArray ja = new JSONObject(response.body().string()).getJSONArray("data");
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject jo = ja.getJSONObject(i);
                            data.add(new MomentBean(jo.getString("content"),jo.getString("account"),jo.getString("files"),jo.getString("createTime"),jo.getString("id")));
                        }
                        adp.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
}